//
//  BJHomePageViewController.m
//  JuJinHuiRong
//
//  Created by APPLE on 16/11/3.
//  Copyright © 2016年 APPLE. All rights reserved.
//

#import "BJHomePageViewController.h"
#import "BJFourBtnTableViewCell.h"
#import "BJTipHeaderTableViewCell.h"
#import "BJShiZhengYaoWenTableViewCell.h"
#import "BJJingPinKeTangTabelViewCell.h"
#import "BJDangShiXueXiViewController.h"//党史学习
#import "BJZhuanTiJiaoYuViewController.h"//专题教育
#import "BJJinPinClassViewController.h"//精品课堂
#import "BJLianZhengJiaoYuViewController.h"//廉政教育
#import "BJStudyTestViewController.h"//学习测试
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "TestViewController.h"
#import "BJMessageViewController.h"//消息
#import "BJSouSuoViewController.h"//搜索

@interface BJHomePageViewController ()<UITableViewDelegate, UITableViewDataSource,NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic, retain) UIView * heaView;

/**
 *  图片数组
 */
@property (nonatomic, strong) NSMutableArray *imageArray;

/**
 *  指示label
 */
@property (nonatomic, strong) UILabel *indicateLabel;

@end

@implementation BJHomePageViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"聊城消防手机党校";
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:statusBarView];
    
//    消息
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"xiaoxi"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStyleDone) target:self action:@selector(didClickMessageItem:)];
    
//    搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"search"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStyleDone) target:self action:@selector(didClickSearchItem:)];

//    for (int index = 0; index < 5; index++) {
//        UIImage *image = [UIImage imageNamed:@"wei"];
//    UIImage *image2 = [UIImage imageNamed:@"wei"];
//    UIImage *image3 = [UIImage imageNamed:@"wei"];
//    UIImage *image4 = [UIImage imageNamed:@"wei"];
//
//
////        [self.imageArray addObject:image];
////    }
//    _imageArray = [[NSMutableArray alloc]initWithObjects:image,image2,image3,image4, nil];
//    
    for (int index = 0; index < 5; index++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",index]];
        [self.imageArray addObject:image];
    }
    [self setTableView];
}

#pragma mark ---- 点击“消息”按钮
- (void)didClickMessageItem:(UIBarButtonItem *)item{
    BJMessageViewController *VC = [[BJMessageViewController alloc]init];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark ---- 点击“搜索”按钮
- (void)didClickSearchItem:(UIBarButtonItem *)item{
    BJSouSuoViewController *VC = [[BJSouSuoViewController alloc]init];
    VC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark ---- 设置TbaleView
- (void)setTableView{
    
    _heaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, BILIHEIGHT(170))];
    _heaView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1.0];
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight -TabBar_HEIGHT - 64) style:(UITableViewStylePlain)];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableHeaderView = _heaView;
    
    //    注册cell
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_myTableView registerClass:[BJTipHeaderTableViewCell class] forCellReuseIdentifier:@"BJTipHeaderTableViewCell"];

    [_myTableView registerClass:[BJFourBtnTableViewCell class] forCellReuseIdentifier:@"BJFourBtnTableViewCell"];
    [_myTableView registerClass:[BJShiZhengYaoWenTableViewCell class] forCellReuseIdentifier:@"BJShiZhengYaoWenTableViewCell"];
    [_myTableView registerClass:[BJJingPinKeTangTabelViewCell class] forCellReuseIdentifier:@"BJJingPinKeTangTabelViewCell"];
//    [_myTableView registerClass:[BJLiCaiTableViewCell class] forCellReuseIdentifier:@"BJLiCaiTableViewCell"];
    
    [self.view addSubview:_myTableView];
     [self setupUI];
}

#pragma mark ---- 实现代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 9;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1 || section == 3 || section == 5|| section == 7){
        return 1;
    }else if (section == 2 || section == 6|| section == 8){
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        weakSelf(self);
        BJFourBtnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJFourBtnTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.pushBlock = ^ (NSInteger a){
            if ( a == 1) {//专题教育
                BJZhuanTiJiaoYuViewController *VC = [[BJZhuanTiJiaoYuViewController alloc]init];
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }else if (a == 2){//党史学习
                BJDangShiXueXiViewController *VC = [[BJDangShiXueXiViewController alloc]init];
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];

            }else if (a == 4){//精品课堂
                BJJinPinClassViewController *VC = [[BJJinPinClassViewController alloc]init];
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];

            }else if (a == 3){//廉政教育
                BJLianZhengJiaoYuViewController *VC = [[BJLianZhengJiaoYuViewController alloc]init];
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];

            }else if (a == 5){//时政要闻
               BJZhuanTiJiaoYuViewController *VC = [[BJZhuanTiJiaoYuViewController alloc]init];
                VC.shiZheng = YES;
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];
                
            }else if (a == 6){//学习测试
                BJStudyTestViewController *VC = [[BJStudyTestViewController alloc]init];
                VC.hidesBottomBarWhenPushed = YES;

                [self.navigationController pushViewController:VC animated:YES];
            }else if (a == 7){//党团实务
                BJDangShiXueXiViewController *VC = [[BJDangShiXueXiViewController alloc]init];
                VC.dangTuanShiWu = YES;
                
                VC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }else if (a == 8){//全部
                
            }
        };
        return cell;
    }else if (indexPath.section == 1 || indexPath.section == 3 || indexPath.section == 5 || indexPath.section == 7){
        BJTipHeaderTableViewCell *cell = [[BJTipHeaderTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"BJTipHeaderTableViewCell"];
        if (indexPath.section == 3) {
            cell.nameLabel.text = @"精品课堂";
        }else if (indexPath.section == 5){
            cell.nameLabel.text = @"专题教育";
        }else if (indexPath.section == 7){
            cell.nameLabel.text = @"党史学习";
        
        }
        return cell;
    }else if (indexPath.section == 4){
        BJJingPinKeTangTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJJingPinKeTangTabelViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }else if (indexPath.section == 2 || indexPath.section == 6){
     
        BJShiZhengYaoWenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJShiZhengYaoWenTableViewCell" forIndexPath:indexPath];
        return cell;
    }else{
        BJShiZhengYaoWenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJShiZhengYaoWenTableViewCell" forIndexPath:indexPath];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    150
    if (indexPath.section == 0) {
        return BILIHEIGHT(175);
    }else  if (indexPath.section == 1 || indexPath.section == 3|| indexPath.section == 5 || indexPath.section == 7 ) {
        return BILIHEIGHT(37);
    }else if (indexPath.section == 2 || indexPath.section == 8|| indexPath.section == 6){
        return BILIHEIGHT(90);
    }else{
        return BILIHEIGHT(90 * 2 * 2);
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 7) {
        BJDangShiXueXiViewController *VC = [[BJDangShiXueXiViewController alloc]init];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.section == 5 || indexPath.section == 1){
        BJZhuanTiJiaoYuViewController *VC = [[BJZhuanTiJiaoYuViewController alloc]init];
        VC.hidesBottomBarWhenPushed = YES;
        if (indexPath.section == 1) {
            VC.shiZheng = YES;
        }
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.section == 3){
        BJJinPinClassViewController *VC = [[BJJinPinClassViewController alloc]init];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }
}


#pragma mark --push控制器
- (void)pushVC {
    
    TestViewController *testVC = [[TestViewController alloc] init];
    
    [self.navigationController pushViewController:testVC animated:YES];
}

- (void)setupUI {
    
    
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, BILIHEIGHT(10), ScreenWidth, BILIHEIGHT(150))];
    pageFlowView.backgroundColor = GrayLabel;
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.minimumPageScale = 0.85;
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    
    //提前告诉有多少页
    //    pageFlowView.orginPageCount = self.imageArray.count;
    
    pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 24 - 8, ScreenWidth, 8)];
    pageFlowView.pageControl = pageControl;
    [pageFlowView addSubview:pageControl];
    
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [bottomScrollView addSubview:pageFlowView];
//    bottomScrollView.backgroundColor = [UIColor greenColor];
    [pageFlowView reloadData];
    
    [_heaView addSubview:bottomScrollView];
    
    
    //添加到主view上
//    [_heaView addSubview:self.indicateLabel];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(ScreenWidth - 84, (ScreenWidth - 84) * 9 / 16);
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    DLog(@"点击了第%ld张图",(long)subIndex + 1);
    self.indicateLabel.text = [NSString stringWithFormat:@"点击了第%ld张图",(long)subIndex + 1];
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.imageArray.count;
    
}

- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 84, (ScreenWidth - 84) * 9 / 16)];
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    //在这里下载网络图片
    //  [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:hostUrlsImg,imageDict[@"img"]]] placeholderImage:[UIImage imageNamed:@""]];
    bannerView.mainImageView.image = self.imageArray[index];
    
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
}

#pragma mark --懒加载
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (UILabel *)indicateLabel {
    
    if (_indicateLabel == nil) {
        _indicateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, ScreenWidth, 16)];
        _indicateLabel.textColor = [UIColor blueColor];
        _indicateLabel.font = [UIFont systemFontOfSize:16.0];
        _indicateLabel.textAlignment = NSTextAlignmentCenter;
        _indicateLabel.text = @"指示Label";
    }
    
    return _indicateLabel;
}

-(void)freshHeader{
    
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self.dataArray removeAllObjects];
//        [self requestLiCaiZhuanQuData];
//        
//        sleep(6);
//        [_myTableView.mj_header endRefreshing];
//    }];
//    _myTableView.mj_header = header;
}


-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = YES;
  
}

-(void)viewWillDisappear:(BOOL)animated{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
